package com.rssreader.client.mvc.views;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.rssreader.client.components.NavPanel;
import com.rssreader.client.mvc.controllers.NavController;
import com.rssreader.client.mvc.events.AppEvents;
import com.rssreader.shared.model.Feed;

public class NavView extends View {

    private final NavPanel navPanel = new NavPanel();

    public NavView(NavController navController) {
	super(navController);
    }

    @Override
    protected void handleEvent(AppEvent event) {
	EventType eventType = event.getType();
	if (eventType.equals(AppEvents.Init)) {
	    // 事件聯動，當controller接收到EventType
	    // Init，派發新事件「NavPanelReady」並將NavPanel實體作為event的data
	    Dispatcher.forwardEvent(new AppEvent(AppEvents.NavPanelReady, navPanel));
	} else if (eventType.equals(AppEvents.FeedAdded)) {
	    onFeedAdded(event);
	} else if (eventType.equals(AppEvents.TabSelected)) {
	    onTabSelected(event);
	}
    }

    private void onTabSelected(AppEvent event) {
	Feed feed = (Feed) event.getData();
	navPanel.selectFeed(feed);
    }

    private void onFeedAdded(AppEvent event) {
	// 調用NavPanel的reloadFeeds方法
	navPanel.reloadFeeds();
    }

}
